/*
 * 文件名：QuartzJob.java
 * 版权：Copyright by www.huawei.com
 * 描述：
 * 修改人：Administrator
 * 修改时间：2012-4-27
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.chinasoftosg.sicap.scheduling.job;

import java.io.IOException;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Logger;
import org.xml.sax.SAXException;

public class QuartzJob
{
    
    private ImportLoader importUtil;
    
    /***
     * 日志文件
     */
    private Logger logger = Logger.getLogger(this.getClass());
    
    public void startTask()
    {
        logger.info("================job start======================");
        ConfigLoader loader = new ConfigLoader();//初始化配置文件
        logger.info("================配置文件初始化======================");
        try
        {
            loader.init();
            System.out.println(loader.getConfigList().size());
        }
        catch (ParserConfigurationException e)
        {
            e.printStackTrace();
        }
        catch (SAXException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        for (Map<String, String> map : loader.getConfigList())
        {
            logger.info(map.get("ip")+"：开始下载到本地");
            downloadFromFtp(map);
            logger.info("解析数据");
            importUtil.excute(map.get("local"), loader);//
        }
        logger.info("........任务执行完成........");
        logger.info("============================");
        
    }
    
    /***
     * 
     * Description: FTP下载<br>
     * @param map 
     * @see
     */
    private void downloadFromFtp(Map<String, String> map)
    {
        String ip = map.get("ip");
        String username = map.get("userName");
        String password = map.get("password");
        String remote = map.get("remote");
        String local = map.get("local");
        
        FtpTool util = new FtpTool();
        try
        {
            util.connect(ip, username, password);
            util.download(remote, local);
        }
        catch (IOException e)
        {
            logger.warn("Ftp下载出现错误：" + e.getMessage());
        }
        finally
        {
            util.closeServer();
        }
    }
    
    public ImportLoader getImportUtil()
    {
        return importUtil;
    }
    
    public void setImportUtil(ImportLoader importUtil)
    {
        this.importUtil = importUtil;
    }
    
    public Logger getLogger()
    {
        return logger;
    }
    
    public void setLogger(Logger logger)
    {
        this.logger = logger;
    }
}
